/*
day176
2020.2.20
*/ 
#include<iostream>
#include<map>
#include<cstdlib>
#include<vector>
using namespace std;


class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
    	map<int,int> m;
    	for(int &i : nums) {m[i]++;}
		int size=m.size();
		if(size==0)	return 0;
		if(size==1)
		{
			return m.begin()->first * m.begin()->second;
		}
		if(size==2)
		{
			auto tem1=m.begin();
			int a1=tem1->first * tem1->second;tem1++;
			int a2=tem1->first * tem1->second;
			return a1>a2 ? a1 : a2;
		}
		
		int *arr=(int *)malloc(sizeof(int)*size);
		auto tem=m.begin();
		arr[0]=tem->first * tem->second;
		tem++;
		arr[1]=tem->first * tem->second;
		tem++;
		arr[1]=arr[1] > arr[0] ? arr[1] : arr[0];
		
		for(int i=2;i<size;++i)
		{
			int cur=tem->first * tem->second + arr[i-2];
			arr[i]=arr[i-1]>cur ? arr[i-1] : cur;
			tem++;
		}
		return arr[size-1];
    }
};

int main()
{
	vector<int> nums = {2, 2, 3, 3, 3, 4};
	Solution s;
	cout<<s.deleteAndEarn(nums)<<endl;
	return 0;
}
